perl access elements of an array

174

perl access elements of an array -

# Basic syntax:
my $array_element = $your_array[array_index];
# Note, Perl's use of sigils ($, @, %) can be a little confusing. Even though
# 	your_array is an array, when accessing a single element it's essentially
#	a scalar so you use $ both for the array and the new variable

# Example usage:
# Define array
my @your_array = (2.3, 42, 'Word');
# Select 2nd element (zero-indexed)
my $array_element = $your_array[2];
print "$array_element\n";
--> Word

Comments

Submit
0 Comments